package org.example.login.wx.callback;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.login.wx.vo.WxMsgReqVo;
import org.example.login.wx.vo.WxMsgResVo;
import org.springframework.web.bind.annotation.*;

import java.util.Enumeration;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("wx")
@Slf4j
public class WxCallBackController {

    @GetMapping("callback")
    public String check(HttpServletRequest request) {
        Enumeration<String> parameterNames = request.getParameterNames();
        String res = null;
        Map<String, String[]> parameterMap = request.getParameterMap();
        String[] echostrs = parameterMap.get("echostr");
        if (Objects.isNull(echostrs))
            return null;
        res = echostrs[0];
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            log.info("queryParameter = {}", objectMapper.writeValueAsString(parameterMap));
        } catch (JsonProcessingException e) {
            log.info("jackson parse object to string occur error!");
        }
        return res;
    }

    @PostMapping(path = "callback",
            consumes = {"application/xml", "text/xml"},
            produces = "application/xml;charset=utf-8")
    public WxMsgResVo callBack(@RequestBody WxMsgReqVo wxMsgReqVo) {

        return new WxMsgResVo();
    }


}
